import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn, BeforeInsert, CreateDateColumn, OneToMany } from 'typeorm';
import { uuidv7 } from 'uuidv7';
import { ApartmentUser } from '../../apartment-user/entities/apartment-user.entity';
import { Comment } from './comment.entity';

@Entity('discussion_posts')
export class DiscussionPost {
  @PrimaryColumn('uuid')
  id: string;

  @Column({ nullable: false, comment: '标题' })
  title: string;

  @Column({ nullable: true, default: '这里什么都没有噢', comment: '内容' })
  content?: string;

  @Column({ 
    name: 'category', 
    nullable: true, 
    comment: '分类',
    type: 'enum',
    enum: ['apartment', 'roommate', 'experience', 'question', 'other'],
    default: 'other'
  })
  category?: string;

  @ManyToOne(() => ApartmentUser, { eager: false })
  @JoinColumn({ name: 'author_id' })
  author: ApartmentUser;

  @Column({ name: 'author_id', nullable: false })
  authorId: string;

  @CreateDateColumn({ name: 'created_at', nullable: true, comment: '发帖时间' })
  createdAt?: Date;

  @Column({ name: 'is_hidden', nullable: false, default: false, comment: '是否隐藏' })
  isHidden: boolean;

  @Column({ name: 'view_count', nullable: false, default: 0, comment: '查看次数' })
  viewCount: number;

  @Column({ name: 'comment_count', nullable: false, default: 0, comment: '评论次数' })
  commentCount: number;

  @Column({ name: 'like_count', nullable: false, default: 0, comment: '点赞次数' })
  likeCount: number;

  @OneToMany(() => Comment, comment => comment.post)
  comments: Comment[];

  @BeforeInsert()
  generateId() {
    if (!this.id) {
      this.id = uuidv7();
    }
  }
} 